package keyfun.utils 
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.filesystem.File;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.display.JPEGEncoderOptions;
	import flash.display.PNGEncoderOptions;
	import flash.filesystem.FileMode;
	/**
	 * ...
	 * @author Key Hui
	 */
	public class ImageUtil 
	{
		
		public static function getBitmapData(target:DisplayObject, targetWidth:Number, targetHeight:Number):BitmapData 
		{
			var bmd:BitmapData = new BitmapData(targetWidth, targetHeight, true, 0xFFFFFF);
			bmd.draw(target, null, null, null, null, true);
			return bmd;
		}
		
		public static function saveToPath(bmd:BitmapData, filePath:String):void
		{
			var file:File = new File(filePath);
			
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			var bytes:ByteArray = bmd.encode(bmd.rect, new PNGEncoderOptions());
			stream.writeBytes(bytes, 0, bytes.bytesAvailable);
			stream.close();
		}
		
	}

}